package week2;

import java.util.Scanner;
import java.util.Arrays;

public class week_03 
{
	public static void main(String[] args) 
	{
		Scanner sc = new Scanner(System.in);
		int n = Integer.parseInt(sc.nextLine());
		String[] ids = new String[n];
		String[] ids1 = new String[n];
		for (int i=0;i<n;i++)
		{
			ids[i] = sc.nextLine();
		}
		while(true)
		{
			String choice = sc.next();
			switch(choice)
			{
			case"sort1":
			{
				for(int i=0;i<n;i++)
				{
					ids1[i]=ids[i].substring(6,10)+"-"+ids[i].substring(10,12)+"-"+ids[i].substring(12,14);
				}
				Arrays.sort(ids1);
				for(int i=0;i<n;i++)
				{
					System.out.println(ids1[i]);
				}
				break;
			}
			case"sort2":
			{
				String[] ids2 = new String[n];
				for(int i=0;i<n;i++)
					ids2[i] = ids[i].substring(6,14);
				for(int i=0;i<n;i++)
				{
					for(int j=0;j<n-1;j++)
					{
						String pre = ids2[j];
						int pre1 = Integer.parseInt(pre);
						String next = ids2[j+1];
						int next1 = Integer.parseInt(next);
						if(pre1>next1)
						{
							String temp = ids2[j];
							ids2[j] = ids2[j+1];
							ids2[j+1] = temp;
							String temp2 = ids[j];
							ids[j] = ids[j+1];
							ids[j+1] = temp2;
						}
					}
				}
				for(int i=0;i<n;i++)
					System.out.println(ids[i]);
				break;
			}
			default:
			{
				System.out.println("exit");
				break;
			}
			}
		}
	}
}
